var app = getApp(),
  fn = app.fn;
var QQMapWX = require("../../utils/qqmap-wx-jssdk.js");
var qqmapsdk
Page({
  data: {
    filterData: [
      {
        "type": 'take_off',
        "text": '起送费(单选)',
        "content": [{
          'name': '全部',
          'value': '-1',
          'selected': false
        },
        {
          'name': '0起送费',
          'value': '0',
          'selected': false

        },
        {
          'name': '非0起送费',
          'value': '1',
          'selected': false

        }
        ]
      },
      {
        "type": 'is_guanzhu',
        "text": '关注(单选)',
        "content": [{
          'name': '全部',
          'value': '-1',
          'selected': false

        },
        {
          'name': '不关注',
          'value': '0',
          'selected': false

        },
        {
          'name': '关注',
          'value': '1',
          'selected': false

        }
        ]
      },
      {
        "type": 'favourable',
        "text": '优惠活动(单选)',
        "content": [{
          'name': '全部',
          'value': '-1',
          'selected': false

        },
        {
          'name': '没有优惠活动',
          'value': '0',
          'selected': false

        },
        {
          'name': '有优惠活动',
          'value': '1',
          'selected': false

        }
        ]
      },
      {
        "type": 'tags',
        "text": '店铺标签(单选)',
        "content": [{
          'name': '全部',
          'value': '-1',
          'selected': false

        },
        {
          'name': '没有标签',
          'value': '0',
          'selected': false

        },
        {
          'name': '有标签',
          'value': '1',
          'selected': false

        }
        ]
      },
      {
        "type": 'rank_level',
        "text": '店铺级别(可多选)',
        "more": true,
        "content": [{
          'name': '初级',
          'value': '0',
          'selected': false
        },
        {
          'name': '中级',
          'value': '1',
          'selected': false
        },
        {
          'name': '高级',
          'value': '2',
          'selected': false
        }
        ]
      },
      {
        "type": 'shipping',
        "text": '配送方式(单选)',
        "content": [{
          'name': '全部',
          'value': '-1'
        },
        {
          'name': '平台配送',
          'value': 'tc_express'
        },
        {
          'name': '商家配送',
          'value': 'sj_express'
        }
        ]
      },
    ],
    sortStatus: false,
    filterStatus: false,
    modalStatus: false,
    sortIndex: 0,   //   [综合排序(1) 距离最近(2) 评分最高(3) 筛选(4)]
    sortType: '',
    wd: '',
    jd: '',
    tc_id: '',
    address: '',
    abc: '111',
    banner: {
      data: [],
      reqData: {
        type: 3
      }
    },
    agePro: [],
    hotPro: {
      data: [],
      loading: false,
      loadend: false,
      reqData: {

      }
    }

  },

  showSortClose: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
    console.log(index);
    this.setData({
      sortStatus: (!that.data.sortStatus),
      filterStatus: false,
      sortIndex: index
    });
    if (this.data.sortStatus) {
      // this.showModal();
    } else {
      //  this.hideModal();
    }
  },
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
    
    }
    return {
      title: '同城e优购',
      path: '方便同城用户生活住行',
    }
  },
  onShareAppMessage: function () {

    var that = this;
    return {
      // title: that.data.banner.shop_name,
      // //    desc: that.data.banner.content,
      // imageUrl: that.data.banner.logo,
    }
  },
 
  showFilterClose: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
    this.setData({
      filterStatus: (!that.data.filterStatus),
      sortStatus: false,
      sortIndex: index
    });
    if (this.data.filterStatus) {
      //  this.showModal();
    } else {
 
    }
  }, showFilterClose2: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.id;
    this.setData({
      filterStatus: (!that.data.filterStatus),
      sortStatus: false,
      sortIndex: index
    });
    that.getInitData();
    if (this.data.filterStatus) {
      //this.showModal();
    } else {
      // this.hideModal();
    }
  },
  sortType: function (e) {

    let sortType = e.currentTarget.dataset.type;
    let filterIndex = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let selectIndex = e.currentTarget.dataset.selectindex;

    let filterData = 'filterData[' + index + '].selected';   
    let filter_content = 'filterData[' + index + '].content[' + selectIndex + '].selected'; 

    let moreFilterData = 'filterData[' + index + '].content[' + selectIndex + '].selected';
    let that = this;
   
    if (this.data.sortStatus) {
      this.setData({
        sortStatus: (!that.data.sortStatus),
        filterStatus: false,
        sortType: sortType,
      });

      that.setData({
        "hotPro.reqData.type": sortType,
        "hotPro.reqData.page": 1,
      });
      console.log(that.data.hotPro.reqData.page);
      this.setData({
        sortIndex: index
      });
      that.getInitData();
      this.hideModal();
    }
    that.setData({
      "hotPro.reqData.page": 1,
    });
    switch (sortType) {
      case 'take_off':  
        that.setData({
          "hotPro.reqData.take_off": filterIndex,
        });
        break;
      case 'is_guanzhu':  
        that.setData({

          "hotPro.reqData.is_guanzhu": filterIndex,
        });
        break;
      case 'favourable':  

        that.setData({

          "hotPro.reqData.favourable": filterIndex,
        });
        break;
      case 'tags':  
        that.setData({

          "hotPro.reqData.tags": filterIndex,
        });
        break;
      case 'rank_level': 
        that.setData({

          "hotPro.reqData.rank_level": filterIndex,
        });


        break;
      case 'shipping':  
        that.setData({

          "hotPro.reqData.shipping": filterIndex,
        });


        break;
    }

 
    if (this.data.filterStatus) {
    
      let attmoreFlag = this.data.filterData[index].content[selectIndex].selected; //获取状态 true /false
      if (sortType == 'rank_level') {
        this.setData({
          sortType: sortType,
          [moreFilterData]: !attmoreFlag
        });
        console.log(sortType + '\n' + filterIndex);
        return;
      }
      
      let attFlag = this.data.filterData[index].content[selectIndex].selected; 
      for (var i = 0; i < this.data.filterData[index].content.length; i++) {
        this.data.filterData[index].content[i].selected = false;
      }
      if (attFlag) {


        switch (sortType) {
          case 'take_off':  
            that.setData({
              "hotPro.reqData.take_off": '',
            });
            break;
          case 'is_guanzhu':  
            that.setData({

              "hotPro.reqData.is_guanzhu": '',
            });
            break;
          case 'favourable':  

            that.setData({

              "hotPro.reqData.favourable": '',
            });
            break;
          case 'tags':  
            that.setData({

              "hotPro.reqData.tags": '',
            });
            break;
          case 'rank_level':  
            that.setData({

              "hotPro.reqData.rank_level": '',
            });


            break;
          case 'shipping':  
            that.setData({

              "hotPro.reqData.shipping": '',
            });


            break;
        }

      }
      this.setData({
        sortType: sortType,
        [filterData]: filterIndex,
        [filter_content]: !attFlag
      });
    }
  },
  
  distanceMark: function (e) {



    var that = this;
    that.setData({

      "hotPro.reqData": "",
    });
    let index = e.currentTarget.dataset.id;

    var id = e.currentTarget.dataset.id,
      url;
    switch (id) {
      case '0':  
        url = '../orderList/orderList?orderStatus=0';
        break;
      case '1':  
        url = '../orderList/orderList?orderStatus=1';
        break;
      case '2': 

        that.setData({

          "hotPro.reqData.type": 'distance1',
        });
        break;
      case '3':  
        that.setData({

          "hotPro.reqData.type": 'pingfen',
        });
        break;
      case '4':  
        that.setData({

          "hotPro.reqData.type": 'order_num',
        });


        break;
    }
    this.setData({
      sortIndex: index
    });
    
    that.getInitData();
  },
  onLoad: function () {

    //  app.checkSession();
    // 获取首页数据






    // fn.apiData({
    //   url: 'mini_version.php',
    //   data: {
    //     act:'info',
    //     version: app.globalData.version
    //   },
    //   success: function (res) { 
    //     console.log("aaaa");
    //     if (res.data.code ==0) { 
    //       if (res.data.data.status == '1') {
    //        wx.switchTab({
    //   url: '../index/index'
    // })



    //       } else {
    //         wx.hideToast();

    //       }

    //     }
    //   }
    // });

    // // this.onReady();
    // var self = this;
    // var t = setInterval(function () {
    //   if (app.globalData.uid != null) {
    //     self.getInitData();
    //     self.checkSetting();

    //     clearInterval(t);
    //   }
    // }, 50);

    // this.checkSetting();


  },
  onReady: function () {
   

  

  },


  onPullDownRefresh: function () {
    this.getInitData(function () {
      wx.stopPullDownRefresh();
    });
  },
 
  onReachBottom: function () {
    this.getMore();
  },

  checkSetting: function () {
    var that = this;
    console.log('33');
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          app.checkSession()
        } else {
          wx.navigateTo({
            url: '../userInfoLogin/index',
          })
        }
      }
    })
  },
 
  getInitData: function (callBack) {
    var that = this,
      flag = 0;
    console.log('333');
    
    fn.apiData({
      url: 'take_out.php',
      data: {
        act: 'banner'
      },

      success: function (res) {
        if (res.data) {
          console.log('55555');
          apiFinish();

          that.setData({ 'banner.data': res.data.data });
        }
      }
    });

 
    fn.apiData({
      url: 'take_out.php',
      data: {
        act: 'menu'
      },
      success: function (res) {
        if (res.data.code == 0) {
          apiFinish();

          that.setData({ 'agePro': res.data.data });

        }
      }
    });

  
    that.setData({
      "hotPro.reqData.act": 'takeout_list',
      "hotPro.reqData.lng": that.data.jd,
      "hotPro.reqData.tc_id": that.data.tc_id,
      "hotPro.reqData.location_status": '2',
      "hotPro.reqData.lat": that.data.wd,
    });
    fn.apiData({
      url: 'stores.php',

      data: that.data.hotPro.reqData,
      success: function (res) {
        if (res.data.code == 0) {


          var newData = res.data.data;
          if (newData == null || !newData) {
           
            that.setData({
              'hotPro.data': newData,
              'hotPro.loadend': true
            });
            apiFinish();
          } else {
         

            apiFinish();

            that.setData({
              'hotPro.data': newData,
              'hotPro.loading': false
            });
          }
        }
      }
    });


    function apiFinish() {
      ++flag;
      if (flag >= 3) {
        wx.hideToast();
        if (!!callBack) callBack();
      }
    }
  },






  menuClick: function (e) {
    var that = this;
    // var type = e.currentTarget.dataset.type,
    var id = e.currentTarget.dataset.id;
    that.setData({
      tc_id: id,
      "hotPro.reqData.page": 1,

    });

    that.getInitData();
    //   url = '../';
    // // if (id!='0'){                 // 产品
    // //     url += 'buyCart/buyCart';

    // // }
    // url += '?id=' + id;
    // wx.navigateTo({ url: url })
  },


  
  bannerClick: function (e) {
    var type33 = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    var url = '../';

    switch (type33) {
      case 1:
        
        url += 'buyCart/buyCart';
        break;
    }

    url += '?id=' + id;
    wx.navigateTo({ url: url })
  },

  getMore: function () {
    var that = this,
      pageNo = ++that.data.hotPro.reqData.pageNo;

 
    if (that.data.hotPro.loadend || that.data.hotPro.loading) return false;


    that.setData({
      "loading": true,
      "hotPro.reqData.page": pageNo
    });

   



    fn.apiData({
      url: 'stores.php',
      lng: that.data.jd,
      location_status: '2',
      lat: that.data.wd,
      page: pageNo,
      data: that.data.hotPro.reqData,
      success: function (res) {
        if (res.data.code == 0) {
          var newData = res.data.data;
          if (newData == null || !newData) {
          
            that.setData({
              'hotPro.loadend': true
            });
          } else {
           
            newData = that.data.hotPro.data.concat(newData);
            that.setData({
              'hotPro.data': newData,
              'hotPro.loading': false
            });
          }

       
          wx.hideToast();
        }
      }
    });
  },
  maketel: function (o) {
    var t = '4000990258';
    wx.makePhoneCall({
      phoneNumber: t
    });
  },
  location: function () {
    var o = '23.436715';
    
  
    var t = '116.751870';
   wx.openLocation({
     latitude: parseFloat(o),
     longitude: parseFloat(t),
     address: '外砂镇迎宾路口南侧肯德基旁金丰达B幢',
      name: "同城e优购"
    });
  },

})
